package me.liyin.flowabe.demo1;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

@Slf4j
public class HolidayRequest {

    public static void main(String[] args) {
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                    .setJdbcUrl("jdbc:mysql://192.168.1.30:3306/flowable?autoReconnect=true")
                    .setJdbcUsername("liyin")
                    .setJdbcPassword("iToPXMkwU8SfHcJ")
                    .setJdbcDriver("com.mysql.jdbc.Driver")
                    .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);

        ProcessEngine processEngine = cfg.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .deploy();
//
//
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
//                .deploymentId(deployment.getId())
//                .singleResult();
//
//        log.info("Found process definition[{}]",processDefinition.getName());
//
//        Scanner scanner= new Scanner(System.in);
//
//        System.out.println("Who are you?");
//        String employee = scanner.nextLine();
//
//        System.out.println("How many holidays do you want to request?");
//        Integer nrOfHolidays = Integer.valueOf(scanner.nextLine());
//
//        System.out.println("Why do you need them?");
//        String description = scanner.nextLine();
//
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//
//        Map<String, Object> variables = new HashMap<String, Object>();
//        variables.put("employee", employee);
//        variables.put("nrOfHolidays", nrOfHolidays);
//        variables.put("description", description);
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);

//        TaskService taskService = processEngine.getTaskService();
//        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup("managers").list();
//        System.out.println("You have " + tasks.size() + " tasks:");
//        for (int i=0; i<tasks.size(); i++) {
//            log.info(") task name[{}]",tasks.get(i).getName());
//        }
//
//        log.info("Which task would you like to complete ?");
//        int taskIndex = Integer.valueOf(scanner.nextLine());
//        Task task = tasks.get(taskIndex - 1);
//        Map<String, Object> processVariables = taskService.getVariables(task.getId());
//        log.info("员工[{}] wants [{}] of holidays. Do you approve this?",processVariables.get("employee"),processVariables.get("nrOfHolidays"));
//
//        boolean approved = scanner.nextLine().toLowerCase().equals("y");
//        Map<String,Object> variables = new HashMap<String, Object>();
//        variables.put("approved", approved);
//        taskService.complete(task.getId(), variables);

//        HistoryService historyService = processEngine.getHistoryService();
//        List<HistoricActivityInstance> activities =
//                historyService.createHistoricActivityInstanceQuery()
//                        .processInstanceId("2504")
//                        .finished()
//                        .orderByHistoricActivityInstanceEndTime().asc()
//                        .list();
//
//        for (HistoricActivityInstance activity : activities) {
//            log.info("activityId[{}] took [{}] milliseconds",activity.getActivityId(),activity.getDurationInMillis());
//        }
    }
}
